package DataFile;

import java.nio.ByteBuffer;

import ClassData.FieldInfo;

public class Structure_FieldData extends Structure_Template {

	FieldInfo myFieldInfo;
	private short dataOffset;

	// constantValue
	public Structure_FieldData(FieldInfo myFieldInfo) {
		this.myFieldInfo=myFieldInfo;	
	}

	public short setOffset(short offset) {
		
		dataOffset=offset;
		offset+=myFieldInfo.getDataSize();
	
		return(offset);	
	}

	public short getDataSize() {
		return((short) myFieldInfo.getDataSize());
	}

	/**
	 * <table>
	 *   <thead><tr><th>Attribute</th><th>Symbolic</th><th>Bytes</th></tr></thead>
	 *   <tbody>
	 *   <tr> <td>FieldAddress</td>             <td>INTEGER_WIDTH</td>     <td>4</td> </tr>
	 *   <tr> <td>AccessFlags</td>              <td>SHORT_WIDTH</td>       <td>2</td> </tr>
	 *   <tr> <td>DataOffset</td>               <td>SHORT_WIDTH</td>       <td>2</td> </tr>
	 *   <tr> <td>Constant value reference</td> <td>SHORT_WIDTH</td>       <td>2</td> </tr>
	 *   <tr> <td>Field type</td>               <td>BYTE_WIDTH</td>        <td>1</td> </tr>
	 *   <tr> <td>Dummy</td>                    <td>BYTE_WIDTH</td>        <td>1</td> </tr>
	 *   <tr> <td></td>                         <td></td>              <td><b>12</b></td></tr>
	 *   </tbody>
	 * </table>
	 */
	protected int getMemorySize() {
		int size;
		
		size =INTEGER_WIDTH; // FieldAdress
		size+=SHORT_WIDTH;   // AccesFlags 
		size+=SHORT_WIDTH;   // DataOffset  
		size+=SHORT_WIDTH;   // Constant value ref
		size+=BYTE_WIDTH;    // field type
		size+=BYTE_WIDTH;    // dummy
		 
		return(size);
	}

	protected String getMemoryMapAsString() {
		String returnString;
		
		returnString=" Field - "+myFieldInfo.getFieldName();
		returnString+=": Base Adress - "+getAdressAsHexString(baseAdress);
		returnString+=" : structure size - "+getMemorySize();
		returnString+=" : Data offset - "+dataOffset;
		returnString+=" : Data size - "+myFieldInfo.getDataSize();
		
		returnString+=" : descriptor index - "+myFieldInfo.getDescriptorIndex();
		returnString+=" : name index - "+myFieldInfo.getNameIndex();
		returnString+=" : flags - ("+Integer.toHexString(myFieldInfo.getFlags());
		returnString+=") "+myFieldInfo.getFlagsAsString();
		returnString+="\n";
		return(returnString);
	}

	public int getNameIndex() {
		return(myFieldInfo.getNameIndex());
	}

	public int getDescriptorIndex() {
		return(myFieldInfo.getDescriptorIndex());
	}

	public String getFieldName() {
		return(myFieldInfo.getFieldName());
	}

	public boolean isStatic() {
		return(myFieldInfo.isStatic());	
	}

	/**
	 *  writes data into memory file
	 *  
	 *  @author Tavin
	 *  
	 *  @param fileData buffer for data 
	 */
	public void writeData(ByteBuffer fileData) {
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		fileData.putShort(myFieldInfo.getFlags());               //  2 Bytes: AccesFlags 
		fileData.putShort(dataOffset);					         //  2 Bytes: DataOffset 
		fileData.putShort(myFieldInfo.getConstantValueIndex());	 //  2 Bytes: Constant value ref
		fileData.put(myFieldInfo.getFieldType());                //  1 Byte : FieldType
		fileData.put((byte)0);							         //  1 Byte : Dummy
	}                                                            //------------------------------
}                                                                //  8 Byte
